<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="merchantList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="签约类型" align="center" prop="type">
        <template #default="scope">
          <span v-if="scope.row.type == 0">个体工商户</span>
          <span v-if="scope.row.type == 1">个人小微</span>
        </template>
      </el-table-column>
      <el-table-column label="商户号" align="center" prop="businessCode" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="身份证开始时间" align="center" prop="startTime" />
      <el-table-column label="过期时间" align="center" prop="expireTime" />
      <!-- <el-table-column label="人像面" align="center" prop="frontImage" width="100">
                <template #default="scope">
                  <image-preview :src="scope.row.frontImage" :width="50" :height="50"/>
                </template>
              </el-table-column>
              <el-table-column label="身份证国徽面" align="center" prop="backImage" width="100">
                <template #default="scope">
                  <image-preview :src="scope.row.backImage" :width="50" :height="50"/>
                </template>
              </el-table-column>
              <el-table-column label="营业执照扫描件" align="center" prop="businessImage" width="100">
                <template #default="scope">
                  <image-preview :src="scope.row.businessImage" :width="50" :height="50"/>
                </template>
              </el-table-column> -->
      <el-table-column
        label="营业执照注册号"
        align="center"
        prop="registerNo"
      />
      <el-table-column label="商户名称" align="center" prop="merchant" />
      <el-table-column label="法人名称" align="center" prop="legalName" />
      <el-table-column label="开户名称" align="center" prop="accountName" />
      <el-table-column label="开户银行" align="center" prop="bank" />
      <el-table-column label="银行账号" align="center" prop="accountNo" />
      <el-table-column label="开户银行地区" align="center" prop="bankArea" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="merchant_status" :value="scope.row.status" />
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="changeStatus(scope.row)"
            v-if="scope.row.status == 0"
            >审核</el-button
          >
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['main:merchant:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['main:merchant:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogFormVisible" title="审核" width="500">
      <el-form :model="form">
        <el-form-item
          label="审核结果"
          align="center"
          :label-width="formLabelWidth"
        >
          <el-radio-group v-model="queryParams.status">
            <el-radio value="1" size="large">通过</el-radio>
            <el-radio value="2" size="large">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核内容" :label-width="formLabelWidth">
          <el-input
            v-model="queryParams.memo"
            style="width: 240px"
            :autosize="{ minRows: 2, maxRows: 4 }"
            type="textarea"
            placeholder="请输入审核内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="suChange"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="merchantRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="身份证开始时间" prop="startTime">
          <el-input
            v-model="form.startTime"
            placeholder="请输入身份证开始时间"
          />
        </el-form-item>
        <el-form-item label="过期" prop="expireTime">
          <el-input v-model="form.expireTime" placeholder="请输入过期" />
        </el-form-item>
        <el-form-item label="人像面" prop="frontImage">
          <image-upload v-model="form.frontImage" />
        </el-form-item>
        <el-form-item label="身份证国徽面" prop="backImage">
          <image-upload v-model="form.backImage" />
        </el-form-item>
        <el-form-item label="营业执照扫描件" prop="businessImage">
          <image-upload v-model="form.businessImage" />
        </el-form-item>
        <el-form-item label="营业执照注册号" prop="registerNo">
          <el-input
            v-model="form.registerNo"
            placeholder="请输入营业执照注册号"
          />
        </el-form-item>
        <el-form-item label="商户名称" prop="merchant">
          <el-input v-model="form.merchant" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="法人名称" prop="legalName">
          <el-input v-model="form.legalName" placeholder="请输入法人名称" />
        </el-form-item>
        <el-form-item label="开户名称" prop="accountName">
          <el-input v-model="form.accountName" placeholder="请输入开户名称" />
        </el-form-item>
        <el-form-item label="开户银行" prop="bank">
          <el-input v-model="form.bank" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="银行账号" prop="accountNo">
          <el-input v-model="form.accountNo" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="开户银行地区" prop="bankArea">
          <el-input v-model="form.bankArea" placeholder="请输入开户银行地区" />
        </el-form-item>
        <el-form-item label="更新名称" prop="updateName">
          <el-input v-model="form.updateName" placeholder="请输入更新名称" />
        </el-form-item>
        <el-form-item label="商户号" prop="businessCode">
          <el-input v-model="form.businessCode" placeholder="请输入商户号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Merchant">
import {
  listMerchant,
  getMerchant,
  delMerchant,
  addMerchant,
  updateMerchant,
  addOrUpdate,
} from "@/api/main/merchant";
import { useRouter } from "vue-router";

const { proxy } = getCurrentInstance();
const { merchant_status } = proxy.useDict("merchant_status");
const merchantList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
    phonenumber: null,
    idCard: null,
    startTime: null,
    expireTime: null,
    frontImage: null,
    backImage: null,
    businessImage: null,
    registerNo: null,
    merchant: null,
    legalName: null,
    accountName: null,
    bank: null,
    accountNo: null,
    bankArea: null,
    memo: null,
    status: null,
    createUser: null,
    createName: null,
    updateUser: null,
    updateName: null,
    businessCode: null,
    isApply: null,
    applyMentId: null,
  },
  rules: {
    status: [
      {
        required: true,
        message: "状态(0资料校验中 1待签约 2审核未通过 3已签约)不能为空",
        trigger: "change",
      },
    ],
    delFlag: [
      { required: true, message: "是否删除;0否1是不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);
const router = useRouter();

/** 查询商户列表 */
function getList() {
  loading.value = true;

  console.log();

  listMerchant({
    createUser: router.currentRoute.value.params?.id,
  }).then((response) => {
    merchantList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userName: null,
    phonenumber: null,
    idCard: null,
    startTime: null,
    expireTime: null,
    frontImage: null,
    backImage: null,
    businessImage: null,
    registerNo: null,
    merchant: null,
    legalName: null,
    accountName: null,
    bank: null,
    accountNo: null,
    bankArea: null,
    memo: null,
    status: null,
    delFlag: null,
    createUser: null,
    createName: null,
    createTime: null,
    updateUser: null,
    updateName: null,
    updateTime: null,
    businessCode: null,
    isApply: null,
    applyMentId: null,
  };
  proxy.resetForm("merchantRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商户";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getMerchant(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商户";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["merchantRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateMerchant(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMerchant(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除商户编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delMerchant(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "main/merchant/export",
    {
      ...queryParams.value,
    },
    `merchant_${new Date().getTime()}.xlsx`
  );
}

getList();
const dialogFormVisible = ref(false);
const getIds = ref();
const changeStatus = (e) => {
  getIds.value = e?.id;
  dialogFormVisible.value = !dialogFormVisible.value;
};

const suChange = () => {
  addOrUpdate(
    Object.assign(queryParams.value, {
      id: getIds.value,
    })
  ).then(async (res) => {
    if (res.code == 200) {
      getList();
      queryParams.value.memo = "";
      queryParams.value.status = "";
      dialogFormVisible.value = !dialogFormVisible.value;
    }
  });
};
</script>
